import request from '@/utils/request'
import method from '@/utils/config'

const urlPrefix = 'crm/'

// 请求路径
const contractApi = {
  // 按条件分页查询
  listByParamsForTable: urlPrefix + 'contract/listByParamsForTable',
  // 根据id查询详情
  selectByPrimaryKey: urlPrefix + 'contract/selectByPrimaryKey',
  // 根据id批量删除合同
  deleteBatchByIds: urlPrefix + 'contract/deleteBatchByIds',
  // 变更负责人
  changeOwnerUser: urlPrefix + 'contract/changeOwnerUser',
  // 新增合同
  saveHasKey: urlPrefix + 'contract/saveHasKey',
  // 修改合同
  updateByPrimaryKeySelective: urlPrefix + 'contract/updateByPrimaryKeySelective',
  // 合同作废
  contractDiscard: urlPrefix + 'contract/contractDiscard',
  // 查询合同下回款
  listReceivables: urlPrefix + 'contract/listReceivables',
  // 查询合同下回款计划
  listReceivablesPlan: urlPrefix + 'contract/listReceivablesPlan',
  // 查询合同下发票
  listInvoice: urlPrefix + 'contract/listInvoice',
  // 查询合同下回访
  listReturnVisit: urlPrefix + 'contract/listReturnVisit',
  // 查询合同下关联的产品数据
  listProduct: urlPrefix + 'contract/listProduct'
}

/**
 * 按条件分页查询
 * @param queryParams 多条件查询对象
 * @returns {AxiosPromise}
 */
export function listByParamsForTable(queryParams) {
  return request({
    url: contractApi.listByParamsForTable,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search
    }
  })
}

/**
 * 添加一条记录，返回主键
 * @param data
 * @returns {*}
 */
export function saveHasKey(data) {
  return request({
    url: contractApi.saveHasKey,
    method: method.POST,
    data
  })
}

/**
 * 修改合同
 * @param data
 * @returns {*}
 */
export function updateByPrimaryKeySelective(data) {
  return request({
    url: contractApi.updateByPrimaryKeySelective,
    method: method.PUT,
    data
  })
}

/**
 * 根据主键查询合同
 * @param id 查询参数
 * @returns {*}
 */
export function selectByPrimaryKey(id) {
  return request({
    url: contractApi.selectByPrimaryKey,
    method: method.GET,
    params: {
      contractId: id
    }
  })
}

/**
 * 根据id批量删除
 * @param ids 要删除的id集合
 * @returns {AxiosPromise}
 */
export function deleteBatchByIds(ids) {
  return request({
    url: contractApi.deleteBatchByIds + `/${ids}`,
    method: method.DELETE
  })
}

/**
 * 查询合同下关联产品
 * @param queryParams 查询参数
 * @returns {AxiosPromise}
 */
export function listProductByContract(queryParams) {
  return request({
    url: contractApi.listProduct,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      searchId: queryParams.searchId
    }
  })
}
